# -*- coding: utf-8 -*-
import json

def getCompanyCertList(request):
    #1数据
    request.mysql_52.execute(request.formatSql('''
        select * 
        from middle.company_apt_${data_source_code} a
        left join standard.dict_company_apt b on a.apt_dict_id = b.id
        left join standard.dict_company_apt_level c on a.level_id = c.level_id
        where ('${company_name}' = '' or company_name like '%${company_name}%')
        and ('${cert_dict_id}' = '' or apt_dict_id = '${cert_dict_id}' )
        
        limit ${start} , ${rows} 
    '''))
    rows = request.mysql_52.fetchall() 
    #2页码
    request.mysql_52.execute(request.formatSql('''
        select  count(*) total 
        from middle.company_apt_${data_source_code}
        where ('${company_name}' = '' or company_name like '%${company_name}%')
        and ('${cert_dict_id}' = '' or apt_dict_id = '${cert_dict_id}' )
    '''))
    total = request.mysql_52.fetchone()['total']
    return json.dumps({'rows':rows,'total':total}, ensure_ascii=False)


#删除勾选的资质
def deleteMany(request):
    delete_count = 0
    data_source = request.form['data_source']
    if data_source not in ['small']:
        return ''
    apt_id_list = request.form['apt_id_list']


    for apt_id in apt_id_list.split("^"):

        sql = "delete from company_apt_${data_source}  where apt_id = " + str(apt_id)
        #print(sql)
        request.mysql_52.execute(request.formatSql(sql))
        delete_count = delete_count + request.mysql_52.rowcount

    return str(delete_count)